import React from 'react'

import api from '../../../api/dictionarie';
import { Form, Input, Tooltip, Icon, Cascader, Select, Row, Col, Checkbox, Button,Message } from 'antd';
const FormItem = Form.Item;
const Option = Select.Option;

import './index.less'

class DictForm extends React.Component {
  state = {
    confirmDirty: false,
  };
  handleSubmit = (e) => {
    e.preventDefault();
    this.props.form.validateFieldsAndScroll((err, values) => {   //提交后得到from表单中得值
      if (!err) {
        if(values.id){
            this.updateDict(values);
        }else{
             this.addDict(values);
        }
      }
    });
  }
  handleConfirmBlur = (e) => {
    const value = e.target.value;
    this.setState({ confirmDirty: this.state.confirmDirty || !!value });
  }
  checkPassword = (rule, value, callback) => {
    const form = this.props.form;
    if (value && value !== form.getFieldValue('password')) {
      callback('Two passwords that you enter is inconsistent!');
    } else {
      callback();
    }
  }
  checkConfirm = (rule, value, callback) => {
    const form = this.props.form;
    if (value && this.state.confirmDirty) {
      form.validateFields(['confirm'], { force: true });
    }
    callback();
  }

addDict = (values) => {
    api.post('/addDict', values).then((data) => {
      const {addfinish} = this.props;
      const result=data.data;
      if(result.code==0){
         Message.success('添加成功！');
          addfinish();
      }else{
          Message.error('添加失败！');
      }
    });
  }
  updateDict = (values) => {
    api.put('/updateDict', values).then((data) => {
          const result=data.data;
          const {upfinish} = this.props;
           if(result.code==0){
             Message.success("修改成功");
              upfinish();
           }else{
             Message.error("修改失败");
           }
    });
  }
  render() {
    const { getFieldDecorator,resetFields } = this.props.form;
    const { close , callbackParent } = this.props;
    const record = this.props.rowRecord;
    const Dictsort = this.props.Dictsort;
    const Dicttype = this.props.Dicttype;
    // 把重置表单的方法传给父亲
    callbackParent({
      resetFields:resetFields
    });

    const formItemLayout = {
      labelCol: { span: 6 },
      wrapperCol: { span: 14 },
    };
    const tailFormItemLayout = {
      wrapperCol: {
        xs: {
          span: 14,
          offset: 0,
        },
        sm: {
          span: 14,
          offset: 6,
        },
      },
    };
    const prefixSelector = getFieldDecorator('prefix', {
      initialValue: '86',
    })(
      <Select className="icp-selector">
        <Option value="86">+86</Option>
      </Select>
    );
    return (
      <Form onSubmit={this.handleSubmit}>
        <FormItem>
          {getFieldDecorator('id', {
              initialValue:record ? record.id : ''
          })(
            <Input type="hidden"/>
          )}
        </FormItem>
        <FormItem
          {...formItemLayout}
          label="value"
          hasFeedback
        >
          {getFieldDecorator('value', {
            rules: [ {
              required: true, message: '请输入你的value!', 
            }],
            initialValue:record ? record.value : ''
          })(
            <Input />
          )}
        </FormItem>
        <FormItem
          {...formItemLayout}
          label="label"
          hasFeedback
        >
          {getFieldDecorator('label', {
            rules: [{
              required: true, message: '请输入label!',
            }],
            initialValue:record ? record.label : ''
          })(
            <Input />
          )}
        </FormItem>
         <FormItem
          {...formItemLayout}
          label="type"
          hasFeedback
        >
          {getFieldDecorator('type', {
            rules: [{
              required: true, message: '请输入type!',
            }],
             initialValue:record ? record.type : (Dicttype?Dicttype:'')
          })(
            <Input />
          )}
        </FormItem>
       <FormItem
          {...formItemLayout}
          label="排序号"
          hasFeedback
        >
          {getFieldDecorator('sort', {
            rules: [{
              required: true, message: '请输入排序号!',
            }],
            initialValue:record ? record.sort : (Dictsort?Dictsort:'')
          })(
            <Input />
          )}
        </FormItem>
       <FormItem
          {...formItemLayout}
          label="描述"
          hasFeedback
        >
          {getFieldDecorator('description', {
              initialValue:record ? record.description : ''
          })(
            <Input  />
          )}
        </FormItem>
        <FormItem
          {...formItemLayout}
          label="备注"
          hasFeedback
        >
          {getFieldDecorator('remarks', {
              initialValue:record ? record.remarks : ''
          })(
            <Input  />
          )}
        </FormItem>
        <FormItem {...tailFormItemLayout}>
          <div>
            <Button type="primary" htmlType="submit" style={{marginRight:'10px'}}>确定</Button>
            <Button onClick={close}>取消</Button>
          </div>
        </FormItem>
      </Form>
    );
  }
}

const WrappedDictForm = Form.create()(DictForm);
export default WrappedDictForm
